<?php

error_reporting(0);
session_start();
@header('Content-Type: text/html; charset=UTF-8');
$dataFile = '../App/index/database.php';
$do = isset($_GET['do']) ? $_GET['do'] : '0';
$scriptpath = str_replace('\\', '/', $_SERVER['SCRIPT_NAME']);
$sitepath = substr($scriptpath, 0, strrpos($scriptpath, '/'));
$siteurl = ($_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $sitepath . '/';

function checkfunc($f, $m = false)
{
    if (function_exists($f)) {
        return '<font color="green">可用</font>';
    } else {
        if ($m == false) {
            return '<font color="black">不支持</font>';
        } else {
            return '<font color="red">不支持</font>';
        }
    }
}

function getRandStr($len = 16, $type = 0)
{
    $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    $strlen = strlen($str);
    $randstr = '';
    for ($i = 0; $i < $len; $i++) {
        $randstr .= $str[mt_rand(0, $strlen - 1)];
    }
    if ($type == 1) {
        $randstr = strtoupper($randstr);
    } elseif ($type == 2) {
        $randstr = strtolower($randstr);
    }
    return $randstr;
}

function checkmodel($f, $m = false)
{
    if (extension_loaded($f)) {
        return '<font color="green">可用</font>';
    } else {
        if ($m == false) {
            return '<font color="black">不支持</font>';
        } else {
            return '<font color="red">不支持</font>';
        }
    }
}

function deleteDir($path)
{

    if (is_dir($path)) {
        $dirs = scandir($path);
        foreach ($dirs as $dir) {
            if ($dir != '.' && $dir != '..') {
                $sonDir = $path . '/' . $dir;
                if (is_dir($sonDir)) {
                    deleteDir($sonDir);
                    @rmdir($sonDir);
                } else {
                    @unlink($sonDir);
                }
            }
        }
        @rmdir($path);
    }
}


function make_Hash($value)
{
    return password_hash($value, PASSWORD_DEFAULT);
}

?>


<!DOCTYPE html>
<html lang="zh-CN">
 <head> 
  <meta charset="UTF-8" /> 
  <meta name="renderer" content="webkit" /> 
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /> 
  <meta name="author" content="xinyan" /> 
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" /> 
  <title>织音QQ助手 - 安装引导</title>
  <link rel="shortcut icon" href="/favicon.ico">
  <link rel="stylesheet" type="text/css" href="/Static/pc/user/css/app.min.css" /> 
  <link rel="stylesheet" type="text/css" href="/Static/pc/user/css/style.min.css" /> 
  <link rel="stylesheet" type="text/css" href="/Static/pc/user/css/animate.css" /> 
  <link rel="stylesheet" type="text/css" href="/Static/pc/user/css/iconfont.css" /> 
  <link rel="stylesheet" type="text/css" href="/Static/pc/user/css/bootstrap-slider.min.css" /> 
  <script type="text/javascript" src="/Static/config/js/jquery-2.1.4.min.js"></script> 
  <script type="text/javascript" src="/Static/pc/user/js/jquery.pjax.min.js"></script> 
  <script type="text/javascript" src="//cdn.istt.cn/cdn/ver/1.1/web/layer.min.js"></script>
  <script type="text/javascript" src="/Static/pc/user/js/app.min.js"></script> 
  <script type="text/javascript" src="/Static/pc/user/js/bootstrap-slider.min.js"></script> 
  <script src="https://cdn.bootcss.com/vue/2.6.11/vue.js"></script>
  <script src="https://cdn.bootcss.com/vue/2.6.11/vue.min.js"></script>
  <script type="text/javascript" src="/Static/config/js/main.js"></script> 
 </head> 
 <body class="bg-bai dl"> 
  <div class="g-main bg-bt2"> 
             <div class="g-header bb"> 
    <div class="g-1400 h d-c t-v"> 
     <div class="logo t-v t-c nowrap"><a href="/"><img class="w7 h7 mr3" src="//cdn.istt.cn/cdn/img/dg/img/ioc-p.png"></a><a class="bai t-s" href="/"><p>织音QQ助手</p><p class="mt0 f3 nobold">安装引导</p></a></div>
     <div class="nav"> 
      <ul class="g-nav click-nav t-v fl"> 
	   <li class=""> <a href="https://gitee.com/sexz/Weaving-Tools" class="t-v">码云地址</a> </li> 
       <li class=""> <a href="https://jq.qq.com/?_wv=1027&k=5e3pBAP" class="t-v">QQ交流群</a> </li> 
	   <li> <a href="tencent://AddContact/?fromId=45&fromSubId=1&subcmd=all&uin=2687409344&website=127.0.0.1" class="t-v" data-pjax="">联系开发者</a> </li>
		</ul> 
     </div> 
    </div> 
   </div> 
   <div class="g-header gonggao bb"> 
    <div class="g-1400 h d-c t-v"> 
     <div class="w9 pl4">
      <i class="icon icon-laba hong f5"></i>
     </div> 
     <div>
      本项目已经在码云部署项目,制作不易,希望可以帮忙点点star,如果你喜欢本程序,可以在码云的文档里赞助我。</div> 
    </div> 
   </div>
            
   <div class="g-1400 g-container" id="pjax-container">
        
<div class="bg-bai b rx3" id="vue-page">
<div class="t-v t-t">
        <div class="c-sidebar c-sidebar-h pb5 br">
            <div class="c-sidebar-title t-v hui bb">安装引导页</div>
            <ul class="c-nav click-nav">
                <li class="<?php if($do == '0'){echo 'active';}?>"><a href="?do=0" class="t-v" >
                    <div><i class="icon icon-write"></i></div>
                    <div>安装引导</div>
                </a></li>
				<li class="<?php if($do == '1'){echo 'active';}?>"><a href="?do=1" class="t-v" >
                    <div><i class="icon icon-write"></i></div>
                    <div>环境监察</div>
                </a></li>
				<li class="<?php if($do == '2' || $do == '3'){echo 'active';}?>"><a class="t-v" >
                    <div><i class="icon icon-write"></i></div>
                    <div>数据库配置</div>
                </a></li>
				<li class="<?php if($do == '4'){echo 'active';}?>"><a class="t-v" >
                    <div><i class="icon icon-write"></i></div>
                    <div>导入数据</div>
                </a></li>
				<li class="<?php if($do == '5'){echo 'active';}?>"><a class="t-v" >
                    <div><i class="icon icon-write"></i></div>
                    <div>安装完成</div>
                </a></li>
            </ul>
        </div>
		<div class="c-content bl">
        <?php if ($do == '0') { ?>
			<div class="w h18 t-c mb10" style="margin-top: 150px;"><p class="h11"><i class="icon icon-appreciate f12 nobold qing dl"></i></p><p class="f5 hui">你正在安装织音QQ助手,是否开始引导?</p><p><a class="btn btn-sm w15 btn-qing btn-1 rx1 bold mt5" href="?do=1">下一步</a></p></div>
        <?php } elseif ($do == '1') { ?>
				<div class="g-title t-v pl4 bb">
                    <div class="col-12 h t-v bold">本地环境检测</div>
                </div>
                <table class="table table-striped">
                    <thead>
                    <tr>
                        <th style="width:20%">函数检测</th>
                        <th style="width:15%">需求</th>
                        <th style="width:15%">当前</th>
                        <th style="width:50%">用途</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr>
                        <td>PHP 5.6+</td>
                        <td>必须</td>
                        <td><?php echo phpversion(); ?></td>
                        <td>PHP版本支持</td>
                    </tr>
                    <tr>
                        <td>curl_exec()</td>
                        <td>必须</td>
                        <td><?php echo checkfunc('curl_exec', true); ?></td>
                        <td>抓取网页</td>
                    </tr>
                    <tr>
                        <td>file_get_contents()</td>
                        <td>必须</td>
                        <td><?php echo checkfunc('file_get_contents', true); ?></td>
                        <td>读取文件</td>
                    </tr>
                    <tr>
                        <td>openssl()</td>
                        <td>非必须</td>
                        <td><?php echo checkmodel('openssl', true); ?></td>
                        <td>发送邮件</td>
                    </tr>
                    </tbody>
                </table>
                <hr/>
				<p class="t-c"><a class="btn btn-sm w15 btn-qing btn-1 rx1 bold mt5" href="?do=2">下一步</a></p>
        <?php } elseif ($do == '2') { ?>
				<div class="g-title t-v pl4 bb">
                    <div class="col-12 h t-v bold">数据库和管理员信息配置</div>
                </div>
				<form action="?do=3" method="post">
				<div class="g-content p5 pt8">
                <div class="in-text" style="width:580px;">
                    <dt class="bold pr4">数据库地址：</dt>
                    <dd><input type="text" class="form p0 h11 bb bb-hui" name="db_host" value="127.0.0.1"></dd>
                </div>
                <div class="in-text" style="width:580px;">
                    <dt class="bold pr4">数据库端口：</dt>
                    <dd><input type="text" class="form p0 h11 bb bb-hui" name="db_port" value="3306"></dd>
                </div>
                <div class="in-text" style="width:580px;">
                    <dt class="bold pr4">数据库用户名：</dt>
                    <dd><input type="text" class="form p0 h11 bb bb-hui" name="db_user"></dd>
                </div>
                <div class="in-text" style="width:580px;">
                    <dt class="bold pr4">数据库密码：</dt>
                    <dd><input type="text" class="form p0 h11 bb bb-hui" name="db_pwd"></dd>
                </div>
                <div class="in-text" style="width:580px;">
                    <dt class="bold pr4">数据库名称：</dt>
                    <dd><input type="text" class="form p0 h11 bb bb-hui" name="db_name"></dd>
                </div>
                <div class="in-text" style="width:580px;">
                    <dt class="bold pr4">QQ账户：</dt>
                    <dd><input type="text" class="form p0 h11 bb bb-hui" name="qq"></dd>
                </div>
				<div class="in-text" style="width:580px;">
                    <dt class="bold pr4">邮箱账户：</dt>
                    <dd><input type="text" class="form p0 h11 bb bb-hui" name="mail"></dd>
                </div>
				</div>
				<div class="in-text" style="width:580px;">
				<dd class="t-c"><button class="btn btn-qing btn-md w22 rx1" type="submit" name="submit">保存配置</button></dd>
				</div><br>
				</form>
        <?php } elseif ($do == '3') {
            ?>
                    <?php
                    $db_host = isset($_POST['db_host']) ? $_POST['db_host'] : NULL;
                    $db_port = isset($_POST['db_port']) ? $_POST['db_port'] : NULL;
                    $db_user = isset($_POST['db_user']) ? $_POST['db_user'] : NULL;
                    $db_pwd = isset($_POST['db_pwd']) ? $_POST['db_pwd'] : NULL;
                    $db_name = isset($_POST['db_name']) ? $_POST['db_name'] : NULL;
					$qq = isset($_POST['qq']) ? $_POST['qq'] : NULL;
					$mail = isset($_POST['mail']) ? $_POST['mail'] : NULL;
                    if ($db_host == null || $db_port == null || $db_user == null || $db_name == null) {
                        echo '<div class="w h18 t-c mb10" style="margin-top: 150px;"><p class="h11"><i class="icon icon-roundclose f12 nobold hong dl"></i></p><p class="f5 hui">请确保你的信息填充完整</p><p><a class="btn btn-sm w15 btn-qing btn-1 rx1 bold mt5"  href="javascript:history.back(-1)">上一步</a></p></div>';
                    } else {
                        $mysql['hostname'] = $db_host;
                        $mysql['hostport'] = $db_port;
                        $mysql['database'] = $db_name;
                        $mysql['username'] = $db_user;
                        $mysql['password'] = $db_pwd;
                        try {
                            $db = new PDO("mysql:host=" . $mysql['hostname'] . ";dbname=" . $mysql['database'] . ";port=" . $mysql['hostport'], $mysql['username'], $mysql['password']);
                        } catch (Exception $e) {
                            $errorMsg = '<div class="w h18 t-c mb10" style="margin-top: 150px;"><p class="h11"><i class="icon icon-roundclose f12 nobold hong dl"></i></p><p class="f5 hui">链接数据库失败:' . $e->getMessage() . '</p><p><a class="btn btn-sm w15 btn-qing btn-1 rx1 bold mt5"  href="javascript:history.back(-1)">上一步</a></p></div>';
                            exit($errorMsg);
                        }
                        @file_put_contents($dataFile, '<?php' . PHP_EOL . 'return ' . var_export($mysql, true) . ';' . PHP_EOL . PHP_EOL . '?>');
                        echo '<div class="w h18 t-c mb10" style="margin-top: 150px;"><p class="h11"><i class="icon icon-roundcheck f12 nobold lv dl"></i></p><p class="f5 hui">保存数据库成功，接下来导入数据表</p><p><a class="btn btn-sm w15 btn-qing btn-1 rx1 bold mt5" href="?do=4&qq='.$qq.'&mail='.$mail.'">我要安装</a> <a class="btn btn-sm w15 btn-lv btn-1 rx1 bold mt5" href="?do=update">我要更新</a></p></div>';
                    }
                    ?>
        <?php } elseif ($do == '4') { ?>
                    <?php
                    $db_config = (require $dataFile);
                    if (!$db_config['username'] || !$db_config['database']) {
                        echo '<div class="w h18 t-c mb10" style="margin-top: 150px;"><p class="h11"><i class="icon icon-roundclose f12 nobold hong dl"></i></p><p class="f5 hui">请先填写好数据库并保存后再安装</p><p><a class="btn btn-sm w15 btn-qing btn-1 rx1 bold mt5"  href="javascript:history.back(-1)">上一步</a></p></div>';
                    } else {
                        try {
                            $db = new PDO("mysql:host=" . $db_config['hostname'] . ";dbname=" . $db_config['database'] . ";port=" . $db_config['hostport'], $db_config['username'], $db_config['password']);
                        } catch (Exception $e) {
                            $errorMsg = '<div class="w h18 t-c mb10" style="margin-top: 150px;"><p class="h11"><i class="icon icon-roundclose f12 nobold hong dl"></i></p><p class="f5 hui">链接数据库失败:' . $e->getMessage() . '</p><p><a class="btn btn-sm w15 btn-qing btn-1 rx1 bold mt5"  href="javascript:history.back(-1)">上一步</a></p></div>';
                            exit($errorMsg);
                        }
                        $db->exec("set names utf8");
						$qq = isset($_GET['qq']) ? $_GET['qq'] : NULL;
						$mail = isset($_GET['mail']) ? $_GET['mail'] : NULL;
						$pass = make_Hash('123456');
                        $sqls = file_get_contents('install.sql');
                        $sqls = explode(';', $sqls);
                        $sqls[] = "INSERT INTO `zy_weblist` (`web_id`, `user_qq`, `mail`, `webname`, `title`,`domain`,`start_time`,`end_time`,`prefix`,`web_key`) VALUES ('1', '{$qq}', '{$mail}', '织音QQ助手', '全网最大的秒赞秒评系统', '{$_SERVER['HTTP_HOST']}','" . date("Y-m-d") . "','2020-01-01','zy_','" . getRandStr() . "')";
                        $sqls[] = "INSERT INTO `zy_gg` (`id`, `con`, `type`, `url`, `color`,`date`,`enddate`,`web_id`) VALUES ('1', '点击进入Hello World交流群,关注程序最新动态', '1', 'https://jq.qq.com/?_wv=1027&k=5e3pBAP', '#f30ca8','2019-06-07','2025-06-07','1')";
						$sqls[] = "INSERT INTO `zy_gg` (`id`, `con`, `type`, `url`, `color`,`date`,`enddate`,`web_id`) VALUES ('2', '久梦云提供测试主机，月付主机低至0元,服务器每日进行本地备份+远程备份，数据保留30天，稳定保障用户数据安全', '1', 'https://host.1x7o.cn/', '#f30ca8','2019-06-07','2025-06-07','1')";
						$sqls[] = "INSERT INTO `zy_users` (`uid`,`username`,`password`,`qq`,`nickname`,`mail`,`power`,`regtime`,`regip`) VALUES ('1','admin','{$pass}','{$qq}','admin','{$mail}','9','" . date("Y-m-d H:i:s") . "','127.0.0.1')";
                        $success = 0;
                        $error = 0;
                        $errorMsg = null;
                        foreach ($sqls as $value) {
                            $value = trim($value);
                            if (!empty($value)) {
                                if ($db->exec($value) === false) {
                                    $error++;
                                    $dberror = $db->errorInfo();
                                    $errorMsg .= $dberror[2] . "<br>";
                                } else {
                                    $success++;
                                }
                            }
                        }
                        $step = 3;
                    }
                    if ($step == 3) {
                        echo '<div class="w h18 t-c mb10" style="margin-top: 150px;"><p class="h11"><i class="icon icon-roundcheck f12 nobold lv dl"></i></p><p class="f5 hui">SQL成功' . $success . '句/失败' . $error . '句</p><p><a class="btn btn-sm w15 btn-qing btn-1 rx1 bold mt5" href="?do=5">下一步</a></p></div>';
                    } else {
						echo '<div class="w h18 t-c mb10" style="margin-top: 150px;"><p class="h11"><i class="icon icon-roundclose f12 nobold hong dl"></i></p><p class="f5 hui">SQL成功' . $success . '句/失败' . $error . '句<br>错误信息：' . $errorMsg . '</p><p><a class="btn btn-sm w15 btn-qing btn-1 rx1 bold mt5" href="?do=4">点击重试</a></p></div>
						';
                    }
                    ?>
		<?php } elseif ($do == 'update') { ?>
                    <?php
                    $db_config = (require $dataFile);
                    if (!$db_config['username'] || !$db_config['database']) {
                        echo '<div class="w h18 t-c mb10" style="margin-top: 150px;"><p class="h11"><i class="icon icon-roundclose f12 nobold hong dl"></i></p><p class="f5 hui">请先填写好数据库并保存后再更新</p><p><a class="btn btn-sm w15 btn-qing btn-1 rx1 bold mt5"  href="javascript:history.back(-1)">上一步</a></p></div>';
                    } else {
                        try {
                            $db = new PDO("mysql:host=" . $db_config['hostname'] . ";dbname=" . $db_config['database'] . ";port=" . $db_config['hostport'], $db_config['username'], $db_config['password']);
                        } catch (Exception $e) {
                            $errorMsg = '<div class="w h18 t-c mb10" style="margin-top: 150px;"><p class="h11"><i class="icon icon-roundclose f12 nobold hong dl"></i></p><p class="f5 hui">链接数据库失败:' . $e->getMessage() . '</p><p><a class="btn btn-sm w15 btn-qing btn-1 rx1 bold mt5"  href="javascript:history.back(-1)">上一步</a></p></div>';
                            exit($errorMsg);
                        }
                        $db->exec("set names utf8");
                        $sqls = file_get_contents('update.sql');
						$sqls = explode(';', $sqls);
                        $success = 0;
                        $error = 0;
                        $errorMsg = null;
                        foreach ($sqls as $value) {
                            $value = trim($value);
                            if (!empty($value)) {
                                if ($db->exec($value) === false) {
                                    $error++;
                                    $dberror = $db->errorInfo();
                                    $errorMsg .= $dberror[2] . "<br>";
                                } else {
                                    $success++;
                                }
                            }
                        }
                        $step = 3;
                    }
                    if ($step == 3) {
                        echo '<div class="w h18 t-c mb10" style="margin-top: 150px;"><p class="h11"><i class="icon icon-roundcheck f12 nobold lv dl"></i></p><p class="f5 hui">SQL成功' . $success . '句/失败' . $error . '句</p><p><a class="btn btn-sm w15 btn-qing btn-1 rx1 bold mt5" href="?do=6">下一步</a></p></div>';
                    } else {
						echo '<div class="w h18 t-c mb10" style="margin-top: 150px;"><p class="h11"><i class="icon icon-roundclose f12 nobold hong dl"></i></p><p class="f5 hui">SQL成功' . $success . '句/失败' . $error . '句<br>错误信息：' . $errorMsg . '</p><p><a class="btn btn-sm w15 btn-qing btn-1 rx1 bold mt5" href="?do=6">点击重试</a></p></div>
						';
                    }
                    ?>
            <?php
        } elseif ($do == '5') {
			
            ?>
			<div class="w h18 t-c mb10" style="margin-top: 150px;"><p class="h11"><i class="icon icon-roundcheck f12 nobold lv dl"></i></p><p class="f5 hui">默认账号admin  默认密码123456，请尽快修改密码，感谢你对本程序的支持</p><p><a class="btn btn-sm w15 btn-qing btn-1 rx1 bold mt5" href="/">开始使用</a></p></div>
        <?php deleteDir('../Begin');?>
		<?php
        } elseif ($do == '6') {
			
            ?>
			<div class="w h18 t-c mb10" style="margin-top: 150px;"><p class="h11"><i class="icon icon-roundcheck f12 nobold lv dl"></i></p><p class="f5 hui">你已经成功无缝更新到最新版本了</p><p><a class="btn btn-sm w15 btn-qing btn-1 rx1 bold mt5" href="/">开始使用</a></p></div>
        <?php deleteDir('../Begin');} ?>
    </div>
	</div>
	</div>
    </div>
   
	</div> 
  <div class="g-footer hui"> 
   <div class="g-1400 h d-c t-v t-c"> 
    <div class="hui dk"> 
     <p>织音QQ助手，可能是最不像开源的开源秒赞</p> 
     <p>Copyright &copy; 2020 <a href="https://jq.qq.com/?_wv=1027&k=5e3pBAP">Hello World交流群</a> | 织音QQ助手 开发者QQ:2687409344</p> 
    </div> 
   </div> 
  </div> 
  <script type="text/javascript" src="/Static/pc/user/js/bootstrap.min.js"></script> 
  <span class="ajaxshow" id="ajaxshow"></span> 
  <span class="ajaxshow" id="ajaxload"></span> 
  <span class="ajaxshow" id="designationshow"></span> 
</body>
</html>